﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class AndroidWXTools {
	static AndroidJavaObject _wxApi;
	static AndroidJavaObject WXApi {
		get{
			if (_wxApi == null) {

				AndroidJavaClass unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
				AndroidJavaObject activity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity");
				AndroidJavaClass apiFactory = new AndroidJavaClass ("com.tencent.mm.sdk.openapi.WXAPIFactory");

				_wxApi = apiFactory.CallStatic<AndroidJavaObject> ("createWXAPI",
					activity,
					"wx63917578c09b0e29",
					false
				);
			}
			return _wxApi;
		}
	}

	public static void VisitWxPay(PayRequestJson PayRequest){

		Dictionary<string,string> paras = new Dictionary<string, string>();
		//BY CG:由于wechat android sdk中的变量与签名标志不一致，所以分开写，#前面是签名属性，后面对应的SDK中变量名
		paras.Add ("appid#appId", "wx63917578c09b0e29");
		paras.Add ("partnerid#partnerId", PayRequest.partnerId); //商户号
		paras.Add ("prepayid#prepayId", PayRequest.prepayId);
		paras.Add ("package#packageValue", PayRequest.package); //暂填写固定值Sign=WXPay
		paras.Add ("noncestr#nonceStr", PayRequest.nonceStr); //随机字符串

		paras.Add ("timestamp#timeStamp", PayRequest.timeStamp);
		paras.Add ("sign#sign", PayRequest.sign); //应用签名

		var request = new AndroidJavaObject ("com.tencent.mm.sdk.modelpay.PayReq");
		foreach(var kv in paras){
			request.Set (kv.Key.Split('#')[1], kv.Value);
		}
		bool ret = WXApi.Call<bool>("sendReq", request);
	}
}
